package com.feijia.aijingjingus;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.clj.fastble.conn.BleCharacterCallback;
import com.clj.fastble.conn.BleGattCallback;
import com.clj.fastble.data.ScanResult;
import com.clj.fastble.exception.BleException;

import static com.feijia.aijingjingus.GlobalHelper.AD1;
import static com.feijia.aijingjingus.GlobalHelper.AD2;
import static com.feijia.aijingjingus.GlobalHelper.BleBuffer;
import static com.feijia.aijingjingus.GlobalHelper.BleReadCount;
import static com.feijia.aijingjingus.GlobalHelper.ShowAD;

public class BleTestActivity extends AppCompatActivity {
    public BleManager bleManager;
    public MyBle myBle;
    public Handler handler =new Handler();

    BleGattCallback BleCallback = new BleGattCallback() {
        @Override
        public void onFoundDevice(ScanResult scanResult) {
            System.out.println("Ble找到设备");
        }

        @Override
        public void onConnecting(BluetoothGatt gatt, int status) {
            System.out.println("Ble正在连接设备");
        }

        @Override
        public void onConnectError(BleException exception) {
            System.out.println("Ble连接错误");
        }

        @Override
        public void onConnectSuccess(BluetoothGatt gatt, int status) {
            System.out.println("Ble连接成功");

        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            System.out.println("Ble发现服务");
            handler.post(new Runnable() {
                @Override
                public void run() {
                    setNotify();
                }
            });
        }

        @Override
        public void onDisConnected(BluetoothGatt gatt, int status, BleException exception) {
            System.out.println("Ble断开连接");
            bleManager.scanNameAndConnect("Nordic_Chen",30000,true,BleCallback);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble_test);
/*
        myBle = new MyBle();
        myBle.Init(this);
        myBle.bleManager.scanNameAndConnect("Nordic_Chen",10000,true,myBle.BleCallback);
*/

        bleManager=new BleManager(this);
        bleManager.scanNameAndConnect("Nordic_Chen",30000,true,BleCallback);
        BleReadCount=0;
    }
    public void onClickSend(View view)
    {
        bleManager.writeDevice(
                GlobalHelper.CYUUID_SERVICE.toString(),
                GlobalHelper.CYUUID_TX.toString(),
                "CMD_ON".getBytes(),
                new BleCharacterCallback() {
                    @Override
                    public void onSuccess(BluetoothGattCharacteristic characteristic) {
                        System.out.println("Ble写入数据"+characteristic.getStringValue(0));
                    }
                    @Override
                    public void onFailure(BleException exception) {
                    }
                    @Override
                    public void onInitiatedResult(boolean result) {
                    }
                });
    }
    public void onClickIndicate(View view)
    {
    }
    public void setNotify()
    {
        bleManager.notify(
                GlobalHelper.CYUUID_SERVICE.toString(),
                GlobalHelper.CYUUID_RX.toString(),
                new BleCharacterCallback(){
                    @Override
                    public void onSuccess(BluetoothGattCharacteristic characteristic) {

                        System.out.println("Ble第"+BleReadCount+"次读到数据" + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8,0));
                        BleBuffer[BleReadCount] = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8,0);
                        if (BleReadCount == 7 && BleBuffer[0] == 255 && BleBuffer[1] == 1 && BleBuffer[2] == 8) {
                            int temp = BleBuffer[3];
                            temp = temp << 8;
                            //AD1=BleBuffer[3];
                            AD1 = BleBuffer[4] + temp;
                            temp = BleBuffer[5];
                            temp = temp << 8;
                            AD2 = BleBuffer[6] + temp;
                            //AD2=BleBuffer[5];
                            ShowAD = true;
                            BleReadCount = -1;
                            System.out.println("Ble读到数据AD1:" +AD1+"  AD2:"+AD2);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    TextView textView39 = (TextView)findViewById(R.id.textView39);
                                    textView39.setText(AD1+"");
                                }
                            });
                        }
                        BleReadCount++;
                    }

                    @Override
                    public void onFailure(BleException exception) {
                        System.out.println("Ble读取数据失败"+exception.toString());
                    }

                    @Override
                    public void onInitiatedResult(boolean result) {
                        System.out.println("Ble读取数据结果"+result);
                        if(!result)
                            setNotify();
                    }
                }
        );
    }

}
